package com.project.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.yongzhuzl.commonutil.util.result.ResponseResult;
import com.project.springboot.annotation.LogCostTime;
import com.project.springboot.annotation.ResponseExcel;
import com.project.springboot.entity.Student;
import com.project.springboot.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 学生表(Student)表控制层
 * @author 朱正磊
 * @since 2023-08-16 00:00:20
 */
@RestController
@RequestMapping("/student")
public class StudentController {

    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;

    /**
     * description: 分页查询所有数据
     * @param page 分页对象
     * @param student 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    public ResponseResult<Page<Student>> selectAll(Page<Student> page, Student student) {
        return ResponseResult.buildSuccess(this.studentService.page(page, new QueryWrapper<>(student)));
    }

    /**
     * description: 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne/{id}")
    public ResponseResult<Student> selectOne(@PathVariable Serializable id) {
        return ResponseResult.buildSuccess(this.studentService.getById(id));
    }

    /**
     * description: 新增数据
     * @param student 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    public ResponseResult<Boolean> insert(@RequestBody Student student) {
        return ResponseResult.buildSuccess(this.studentService.save(student));
    }

    /**
     * description: 新增数据2
     * @param student 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert2")
    public ResponseResult<Boolean> insert2(Student student) {
        return ResponseResult.buildSuccess(this.studentService.save(student));
    }

    /**
     * description: 修改数据
     * @param student 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody Student student) {
        return ResponseResult.buildSuccess(this.studentService.updateById(student));
    }

    /**
     * description: 删除数据
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public ResponseResult<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        return ResponseResult.buildSuccess(this.studentService.removeByIds(idList));
    }

    /**
     * description: 批量插入
     * @author 朱正磊
     * @date 2023-08-17 12:23:50
     * @return 返回响应结果
     */
    @GetMapping("/batchInsert")
    public ResponseResult<Void> batchInsert() {
        studentService.batchInsert();
        return ResponseResult.buildSuccess();
    }

    /**
     * description: 导出Excel（方式一）
     * @author 朱正磊
     * @date 2023-08-17 12:24:27
     * @return 返回导出数据
     */
    @GetMapping("/exportExcel")
    @ResponseExcel(name = "测试", sheetName = "测试sheet")
    public List<Student> exportExcel() {
        List<Student> studentList = studentService.getStudentList();
        System.out.println("studentList.size = " + studentList.size());
        return studentList;
    }

    /**
     * description: 导出Excel（方式二）
     * @author 朱正磊
     * @date 2023-08-31 16:03:31
     * @param response 响应对象
     */
    @GetMapping("/exportExcel2")
    public void exportExcel2(HttpServletResponse response) {
        studentService.exportExcel2(response);
    }

    /**
     * description: 测试异步线程（使用CompletableFuture.runAsync方法）
     * @author 朱正磊
     * @date 2025-06-04 15:12:04
     * @param id 主键
     * @return 返回响应结果
     */
    @LogCostTime(value = "使用CompletableFuture.runAsync方法测试异步线程", consumeWarn = 20)
    @GetMapping("/selectById/{id}")
    public ResponseResult<Student> selectById(@PathVariable Integer id) {
        Student student = studentService.selectById(id);
        return ResponseResult.buildSuccess(student);
    }

    /**
     * description: 测试异步线程（使用@Async注解）
     * @author 朱正磊
     * @date 2025-06-05 15:12:04
     * @param id 主键
     * @return 返回响应结果
     */
    @LogCostTime(value = "使用@Async注解测试异步线程", consumeWarn = 20)
    @GetMapping("/findById/{id}")
    public ResponseResult<Student> findById(@PathVariable Integer id) {
        long start = System.currentTimeMillis();
        Student student = studentService.getById(id);

        // 异步任务
        CompletableFuture<String> future = studentService.insertOne();

        // 线程阻塞
        // String msg = future.join();
        // msg = 插入一条学生信息的异步任务完成
        // System.out.println("msg = " + msg);

        long end = System.currentTimeMillis();
        long interval = end - start;
        System.out.println("耗时 = " + interval + "毫秒");

        return ResponseResult.buildSuccess(student);
    }

    /**
     * description: 批量保存
     * @author 朱正磊
     * @date 2025-06-11 13:24:01
     * @return 无业务返回值
     */
    @PostMapping("/batchSave")
    public ResponseResult<Void> batchSave() {
        studentService.batchSave();
        return ResponseResult.buildSuccess();
    }

    /**
     * 提醒：不存在上传文件
     * 说明：该接口属于多功能接口，同时支持get请求，post请求，form传参，json传参
     * description: 根据条件查询学生列表
     * @author 朱正磊
     * @date 2025-11-27 08:12:59
     * @return 返回学生列表
     */
    @RequestMapping("/selectStudentList1")
    public ResponseResult<List<Student>> selectStudentList1() {
        List<Student> studentList = studentService.selectStudentList1();
        return ResponseResult.buildSuccess(studentList);
    }

    /**
     * 提醒：存在上传文件
     * 说明：该接口属于多功能接口，同时支持get请求，post请求，form传参，json传参
     * description: 根据条件查询学生列表
     * @author 朱正磊
     * @date 2025-11-27 08:12:59
     * @return 返回学生列表
     */
    @RequestMapping("/selectStudentList2")
    public ResponseResult<List<Student>> selectStudentList2() {
        List<Student> studentList = studentService.selectStudentList2();
        return ResponseResult.buildSuccess(studentList);
    }

    /**
     * description: 根据条件查询学生列表（测试multipart/form-data）
     * @author 朱正磊
     * @date 2025-11-27 17:09:32
     * @return 返回学生列表
     */
    @RequestMapping("/selectStudentList3")
    public ResponseResult<List<Student>> selectStudentList3() {
        List<Student> studentList = studentService.selectStudentList3();
        return ResponseResult.buildSuccess(studentList);
    }

    /**
     * description: 根据条件查询学生列表（测试application/json）
     * @author 朱正磊
     * @date 2025-11-27 17:09:51
     * @return 返回学生列表
     */
    @RequestMapping("/selectStudentList4")
    public ResponseResult<List<Student>> selectStudentList4() {
        List<Student> studentList = studentService.selectStudentList4();
        return ResponseResult.buildSuccess(studentList);
    }

}
